package com.mayikt.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mayikt.base.BaseApiService;
import com.mayikt.base.BaseResponse;
import com.mayikt.constants.Constants;
import com.mayikt.core.util.GenerateTokenUtils;
import com.mayikt.member.input.dto.UserLoginInpDTO;
import com.mayikt.member.mapper.UserMapper;
import com.mayikt.member.mapper.entity.UserEntity;
import com.mayikt.member.output.dto.UserOutDTO;
import com.mayikt.member.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Service
public class MemberServiceImpl extends BaseApiService<UserOutDTO> implements MemberService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GenerateTokenUtils generateTokenUtils;

    @Override
    public BaseResponse<UserOutDTO> existMobile(String mobile) {
        int x = 1/0;
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(UserEntity::getMobile,mobile);
        UserEntity userEntity = userMapper.selectOne(queryWrapper);
        if (null == userEntity){
            return setResultError(Constants.HTTP_RES_CODE_EXISTMOBILE_203,"用户信息不存在！");
        }
        //去敏操作
        UserOutDTO userOutDTO = new UserOutDTO();
        BeanUtils.copyProperties(userEntity,userOutDTO);
        return setResultSuccess(userOutDTO);
    }

    @Override
    public BaseResponse<UserOutDTO> getInfo(String token) {
        if (StrUtil.isEmpty(token)){
            return setResultError("token不能为空！");
        }
        try {
            Long userId = Long.parseLong(generateTokenUtils.getToken(token));
            if (null != userId && null != userMapper.selectById(userId)){
                UserEntity userEntity = userMapper.selectById(userId);
                UserOutDTO userOutDTO = new UserOutDTO();
                BeanUtils.copyProperties(userEntity,userOutDTO);
                return setResultSuccess(userOutDTO);
            } else {
                return setResultError("token无效或者已过期！");
            }
        } catch (NumberFormatException e){
            return setResultError("token无效或者已过期！");
        }
    }
    //登陆返回用户信息
    @Override
    public BaseResponse<UserOutDTO> ssoLogin(@RequestBody UserLoginInpDTO userLoginInpDTO) {
        if (StrUtil.isEmpty(userLoginInpDTO.getMobile())){
            return setResultError("手机号不能为空！");
        }
        if (StrUtil.isEmpty(userLoginInpDTO.getPassword())){
            return setResultError("密码不能为空！");
        }
//        2.密码加密
        String password = SecureUtil.md5(userLoginInpDTO.getPassword());
//        3.使用手机号+密码登陆
        UserEntity userEntity = selectUserByUserNameAndPassword(userLoginInpDTO.getMobile(),password);
        if (null == userEntity){
            return setResultError("用户名或密码错误！");
        }
        UserOutDTO userOutDTO = new UserOutDTO();
        BeanUtils.copyProperties(userEntity,userOutDTO);
        return setResultSuccess(userOutDTO);
    }

    public UserEntity selectUserByMobileAndPassword(String mobile,String password){
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserEntity::getMobile,mobile).eq(UserEntity::getPassword,password);
        return userMapper.selectOne(queryWrapper);
    }
    public UserEntity selectUserByUserNameAndPassword(String username,String password){
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserEntity::getUserName,username).eq(UserEntity::getPassword,password);
        return userMapper.selectOne(queryWrapper);
    }
}
